package com.ruoyi.develop.goodsFee.service.impl;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;

import javax.annotation.Resource;

import com.ruoyi.develop.goodsInvoiceSelf.service.IGoodsInvoiceSelfService;
import com.ruoyi.develop.projectBaseInfo.domain.ProjectBaseInfo;
import com.ruoyi.develop.projectBaseInfo.service.IProjectBaseInfoService;
import org.springframework.stereotype.Service;
import com.ruoyi.develop.goodsFee.mapper.GoodsFeeMapper;
import com.ruoyi.develop.goodsFee.domain.GoodsFee;
import com.ruoyi.develop.goodsFee.service.IGoodsFeeService;

/**
 * 研发经费Service业务层处理
 *
 * @author qh
 * @date 2023-11-10
 */
@Service
public class GoodsFeeServiceImpl implements IGoodsFeeService {
    @Resource
    private GoodsFeeMapper goodsFeeMapper;
    @Resource
    private IProjectBaseInfoService baseInfoService;
    @Resource
    private IGoodsInvoiceSelfService invoiceSelfService;

    /**
     * 查询研发经费
     *
     * @param id 研发经费主键
     * @return 研发经费
     */
    @Override
    public GoodsFee selectById(String id) {
        return goodsFeeMapper.selectById(id);
    }

    /**
     * 查询研发经费列表
     *
     * @param entity 研发经费
     * @return 研发经费
     */
    @Override
    @DataScope(deptAlias = "d")
    public List<GoodsFee> selectList(GoodsFee entity) {
        return goodsFeeMapper.selectList(entity);
    }

    @Override
    public List<GoodsFee> selectListNoScope(GoodsFee entity) {
        return goodsFeeMapper.selectList(entity);
    }

    /**
     * 新增研发经费
     *
     * @param entity 研发经费
     * @return 结果
     */
    @Override
    public int insert(GoodsFee entity) {
        if (StringUtils.isBlank(entity.getId())) {
            entity.setId(IdUtils.fastSimpleUUID());
        }
        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        int i= goodsFeeMapper.insert(entity);
        updateProjectFee(entity.getId());
        if("1".equals(entity.getInvoice())){//如果有发票,则生成发票信息
            invoiceSelfService.createInvoiceByFee(entity);
        }
        return i;
    }

    /**
     * 修改研发经费
     *
     * @param entity 研发经费
     * @return 结果
     */
    @Override
    public int update(GoodsFee entity) {
        entity.setUpdateBy(SecurityUtils.getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        int i = goodsFeeMapper.update(entity);
        updateProjectFee(entity.getId());
        return i;
    }

    /**
     * 批量删除研发经费
     *
     * @param ids 需要删除的研发经费主键
     * @return 结果
     */
    @Override
    public int deleteByIds(String[] ids) {
        int i = goodsFeeMapper.deleteByIds(ids);
        updateProjectFee(ids[0]);
        return i;
    }

    /**
     * 删除研发经费信息
     *
     * @param id 研发经费主键
     * @return 结果
     */
    @Override
    public int deleteById(String id) {
        int i = goodsFeeMapper.deleteById(id);
        updateProjectFee(id);
        return i;
    }

    @Override
    public void updateImburseIdForFee(GoodsFee fee) {
        goodsFeeMapper.updateImburseIdForFee(fee);
    }

    public void updateProjectFee(String feeId){
        //找到研发经费
        GoodsFee fee = selectById(feeId);
        //找到项目信息
//        ProjectBaseInfo project = baseInfoService.selectById(fee.getProId());
        if(StringUtils.isNotEmpty(fee.getProId())){
            ProjectBaseInfo project = new ProjectBaseInfo();
            project.setId(fee.getProId());
            //重新计算项目费用(改项目计算逻辑)
            baseInfoService.refreshProfit(project);
        }
        //重新计算项目利润(改项目计算逻辑)
    }

    @Override
    public void updateImburseIdForFeeByIds(Map<String, String> orderPara) {
        goodsFeeMapper.updateImburseIdForFeeByIds(orderPara);
    }

    @Override
    public void updateFeeStatusByImburseId(GoodsFee feeParam) {
        goodsFeeMapper.updateFeeStatusByImburseId(feeParam);
    }


}
